package com.ruoyi.system.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.domain.request.UserRequest;
import com.ruoyi.system.service.RegistrationService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 姚炜琦
 * @Classname RegistrationController
 * @Description TODO
 * @Date 2022/10/21 9:14
 */
@Controller
@RequestMapping("/registrationClient")
@Log4j2
public class RegistrationController {
    @Autowired
    private RegistrationService registrationService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据用户手机号获取登录校验验证码
     * @param phone
     * @return
     */
    @PostMapping("/getCode/{phone}")
    @ResponseBody
    public String getCode(@PathVariable("phone") String phone) {
        log.info("功能：【{}】，请求地址：【{}】，请求方式：【{}】，请求参数：【{}】",
                "根据用户手机号获取登录校验验证码",request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(phone));

        String code = registrationService.getCode(phone);

        log.info("功能：【{}】，请求地址：【{}】，请求方式：【{}】，响应结果：【{}】",
                "根据用户手机号获取登录校验验证码",request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(code));
        return code;
    }

    /**
     * 新用户注册
     * @param userRequest
     * @return
     */
    @RequestMapping(value = "/addUser")
    @ResponseBody
    public String addUser(@RequestBody UserRequest userRequest) {
        log.info("功能：【{}】，请求地址：【{}】，请求方式：【{}】，请求参数：【{}】",
                "新用户注册",request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(userRequest));

        String i = registrationService.addUser(userRequest);
        String token = JSONObject.toJSONString(i);

        log.info("功能：【{}】，请求地址：【{}】，请求方式：【{}】，响应结果：【{}】",
                "新用户注册",request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(token));
        return token;
    }
}
